<?php

namespace app\api\model;

use app\common\enums\StatusEnum;

class TieBaCollect extends \app\common\model\TieBaCollect
{
    /**
     * 我的收藏贴
     * @param $oauthId
     * @param int $type 类型，1点赞，2收藏
     * @param $page
     * @param $size
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList($oauthId, $type, $page, $size)
    {
        $offset = ($page - 1) * $size;

        $sql = static::field('t.id AS tieId, o.id AS uid, o.nickname, o.head_img AS headImg,o.phone, tt.name, t.content, t.praise, t.is_top AS isTop, t.create_time AS createTime')
            ->alias('ot')
            ->join('tie_ba t', 'ot.tie_id = t.id')
            ->join('oauth o', 't.oauth_id = o.id')
            ->join('tie_ba_type tt', 't.type_id = tt.id')
            ->where([
                'ot.oauth_id' => $oauthId,
                'ot.type' => $type,
                't.is_draft' => TieBa::DRAFT_NO,
                't.check_status' => TieBa::CHECK_PASS,
                'tt.status' => StatusEnum::ENABLED,
                'o.status' => StatusEnum::ENABLED
            ]);

        $model = $sql->order(['t.is_top' => 'desc', 't.create_time' => 'desc'])
            ->limit($offset, $size)
            ->select()
            ->toArray();

        $model = TieBa::getTieData($oauthId, $model);

        return [
            'list' => $model,
            'count' => $sql->count()
        ];
    }

    /**
     * 获取一条点赞或者收藏记录
     * @param $oauthId
     * @param $tieId
     * @param $type
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getOne($oauthId, $tieId, $type)
    {
        return static::field('id')
            ->where([
                'oauth_id' => $oauthId,
                'tie_id' => $tieId,
                'type' => $type
            ])->find();
    }

    /**
     * 个人 点赞的 或者 收藏的 帖子
     * @param $oauthId
     * @param $tieIds
     * @param $type
     * @return array
     */
    public static function getByTie($oauthId, $tieIds, $type)
    {
        return static::field('id')
            ->where([
                'oauth_id' => $oauthId,
                'type' => $type
            ])->where('tie_id', 'in', $tieIds)
            ->column('id', 'tie_id');
    }

    /**
     * 添加一条记录
     * @param $oauthId
     * @param $tieId
     * @param $type
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function addOne($oauthId, $tieId, $type)
    {
        $model = static::create([
            'type' => $type,
            'oauth_id' => $oauthId,
            'tie_id' => $tieId
        ]);

        // 更新点赞数
        if ($type == self::TYPE_PRAISE) {
            TieBa::updatePraise($tieId, 1);
        }

        return isset($model->id) ? true : false;
    }

    /**
     * 取消点赞或者收藏
     * @param $oauthId
     * @param $tieId
     * @param $type
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function deleteOne($oauthId, $tieId, $type)
    {
        $delete = static::where([
            'type' => $type,
            'oauth_id' => $oauthId,
            'tie_id' => $tieId
        ])->delete();

        // 扣除点赞数
        if ($delete > 0 && $type == self::TYPE_PRAISE) {
            TieBa::updatePraise($tieId, -1);
        }
    }

    /**
     * 我的收藏帖子数量
     * @param $oauthId
     * @param int $type 类型，1点赞，2收藏
     * @return float|string
     */
    public static function getCountMine($oauthId, $type = self::TYPE_PRAISE)
    {
        return static::field('t.id')
            ->alias('c')
            ->join('tie_ba t', 'c.tie_id = t.id')
            ->where([
                'c.type' => $type,
                'c.oauth_id' => $oauthId,
                't.is_draft' => TieBa::DRAFT_NO,
                't.check_status' => TieBa::CHECK_PASS,
            ])->count();
    }
}